﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class SignUp : System.Web.UI.Page
{
    UsersBLL usersLogic = new UsersBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtEmail.Focus();
        }
    }

    protected void btnSignUpOK_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string Email = txtEmail.Text;
            string hasedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "sha1");
            string FirstName = txtFirstName.Text;
            string LastName = txtLastName.Text;
            string Organization = txtOrganization.Text;
            int StudentYear = 0;
            string SecurityQuestion = txtSecurityQuestion.Text;
            string SecurityAnswer = txtSecurityAnswer.Text;

            if (usersLogic.IsUserInTable(Email) == 1)
            {
                string _msg = "The email address provided has already been registered. Please try again.";
                ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
            }
            else
            {
                bool result = usersLogic.InsertNonInstitutUser(Email, hasedPassword,
                    "NonInstitut", FirstName, LastName, Organization, StudentYear,
                    SecurityQuestion, SecurityAnswer) != 0;

                if (result)
                {
                    string _msg = "A message has been sent to the email address provided, containing information on how to <strong>activate</strong> your account.";
                    ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", _msg, 1, "ActivateAccount.aspx?Email=" + Email);
                }
                else
                {
                    // *SteveMsg
                    string _msg = "Registration Failed. Please Try Again.";
                    ModalPopupMessage.ShowModalPopup("ILE Message", _msg, 2);
                }
            }
        }
    }

    protected void ddlistSecurityQuestionExamples_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlistSecurityQuestionExamples.SelectedIndex >= 1)
        {
            txtSecurityQuestion.Text = ddlistSecurityQuestionExamples.SelectedValue;
        }
        else
        {
            txtSecurityQuestion.Text = String.Empty;
        }
    }

    protected void customValidatorSecurityAnswer_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 4)
        {
            args.IsValid = false;
            return;
        }

        args.IsValid = true;
    }
}
